package com.tjetc.view.impl;

import com.tjetc.constant.CommonConstant;
import com.tjetc.exception.MessageException;
import com.tjetc.pojo.dto.AdminDTO;
import com.tjetc.pojo.dto.UserDTO;
import com.tjetc.utils.EmailUtil;
import com.tjetc.utils.RandomCodeUtil;
import com.tjetc.utils.RedisUtils;
import com.tjetc.utils.ScannerUtils;
import com.tjetc.view.RegisterPage;
import redis.clients.jedis.Jedis;

import java.util.UUID;

/**
 * @Author: itda
 * @Date: 2024/5/9 14:26
 * @Description: RegisterPageImpl
 */
public class RegisterPageImpl implements RegisterPage {
    @Override
    public UserDTO register() {
        System.out.println("请输入用户名:");
        String username = ScannerUtils.sc.next();
        System.out.println("请输入密码:");
        String password = ScannerUtils.sc.next();
        System.out.println("请输入邮箱:");
        String email = ScannerUtils.sc.next();
        System.out.println("请输入性别(男1/女0):");
        short gender = ScannerUtils.sc.nextShort();
        System.out.println("请输入手机号:");
        String phone = ScannerUtils.sc.next();
        String randomNumber = RandomCodeUtil.getRandomNumber(4);
        UUID uuid = UUID.randomUUID();
        Jedis jedis = RedisUtils.getJedis();
        jedis.setex(uuid + "code", 60, randomNumber);
        EmailUtil.sendEmail(email, CommonConstant.CODE_NOTICE, "验证码为：" + randomNumber + CommonConstant.CODE_EFFECTIVE_TIME);
        System.out.println("请输入验证码：");
        String code = ScannerUtils.sc.next();
        if (!code.equals(jedis.get(uuid +"code"))) {
            try {
                throw new MessageException(CommonConstant.CODE_ERROR);
            } catch (MessageException e) {
                System.out.println(e.getMessage());
            }
        }
        return UserDTO.builder()
                .username(username)
                .password(password)
                .email(email)
                .gender(gender)
                .phone(phone)
                .build();
    }
}
